//
//  XYPhotoImageView.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/22.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

class XYPhotoImageView: UIImageView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        //k开启用户交互
        self.userInteractionEnabled = true
        //显示模式
        contentMode = UIViewContentMode.ScaleAspectFill
        clipsToBounds = true
        
        addSubview(remvoButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var remvoButton:UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "didButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.sizeToFit()
        
        return button
        }()
    
    @objc private func didButton(){
        
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            self.alpha = 0.0
        }) { (finish) -> Void in
            self.removeFromSuperview()
        }
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        remvoButton.x = self.width - remvoButton.width
        
    }

}
